package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.family.LoanNewCreditCardRebate;
import com.tuanzi.loan.business.service.family.LoanNewCreditCardOrderService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.LoanNewCreditCardOrderRequest;
import com.tuanzi.loan.vo.response.family.LoanNewCreditCardOrderResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanNewCreditCardOrderLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-09-13 14:10
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanNewCreditCardOrderListBean extends JSFListingBean<LoanNewCreditCardOrderRequest,LoanNewCreditCardOrderResponse> {

    @Autowired
    private LoanNewCreditCardOrderService loanNewCreditCardOrderService;

    List<LoanNewCreditCardRebate> rebateList;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        rebateList = new ArrayList<>();
        $ = new LoanNewCreditCardOrderRequest();
        lazyDataModel = new LoanNewCreditCardOrderLazyDataModel($);
    }

    public void onReset(){
        initLazyDataModel();
    }

    public void handleImportRebate(FileUploadEvent event) {
        InputStream inputstream = null;
        try {
            inputstream = event.getFile().getInputstream();
            if (inputstream == null) {
                addErrorMessage("文件为空");
                return;
            }
            //处理excel
            handleExcel(inputstream);
            update("creditCardOrder:rebate-dialog");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputstream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void onImport(){
        try {
            loanNewCreditCardOrderService.onImportRebate(rebateList);
            rebateList = new ArrayList<>();
            addSuccessfullyMessage();
        } catch (Exception e) {
            addErrorMessage("导入失败,请联系管理员");
            e.printStackTrace();
        }finally {
            execute("PF('doing').hide()");
            execute("PF('rebateDialog').hide()");
            update("creditCardOrder:statisticRecordDataTable");
        }
    }

    private void handleExcel(InputStream is) throws Exception{
        Workbook workbook = WorkbookFactory.create(is);
        Sheet sheet = workbook.getSheetAt(0);
        rebateList = new ArrayList<>();
        LoanNewCreditCardRebate rebate = null;
        /** 得到Excel的总行数 */
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int i=1;i<rowCount;i++){
            Row row = sheet.getRow(i);
            if (row == null) continue;
            rebate = new LoanNewCreditCardRebate();
            for (int j=0;j<row.getLastCellNum();j++){
                Cell cell = row.getCell(j);
                String stringCellValue="";
                switch (cell.getCellType()){
                    case Cell.CELL_TYPE_STRING:
                        stringCellValue = cell.getStringCellValue();
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        stringCellValue = cell.getNumericCellValue()+"";

                }
                if (StringUtils.isBlank(stringCellValue)) {
                    rebate = null;
                    break;
                }
                switch (j){
                    case 0:
                        rebate.setCardId((long)Double.parseDouble(stringCellValue));
                        break;
                    case 1:
                        rebate.setMobnum(stringCellValue.trim());
                        break;
                    case 2:
                        rebate.setUsername(stringCellValue.trim());
                        break;
                }
            }
            if (rebate != null) rebateList.add(rebate);
        }
    }
}
